var keyData = getQrkey();

if(keyData.qrKey != '' && keyData.type == 1) {
    location.href = location.protocol+'//'+location.host+location.pathname + '?_=' + timestamp;
}

if(keyData.qrKey) {
    getSliders();
}

mui.init({
    swipeBack: false,
});
//侧滑容器父节点
var offCanvasWrapper = mui('#offCanvasWrapper');

// 获取qrkey值
function getQrkey(){
    var key = '';
    var type = 1; // 不需要刷新页面
    if(GetQueryString('qrKey') != null){
        key = GetQueryString('qrKey');
        var currentTime = new  Date().getTime();
        setCache('qr_key_value',key);
        setCache('qr_key_expires', currentTime);
    } else {
        type = 0;
        if(getCache('qr_key_expires')) {
            var nowTime = new Date().getTime();
            var expires = getCache('qr_key_expires');
            // 判断缓存是否大于一天
            if(nowTime - expires > 1000 * 3600 * 24) {
                deleteCache('qr_key_value');
                deleteCache('qr_key_expires');
            } else {
                key = getCache('qr_key_value')
            }
        }
    }

    if(key == '') {
        malert('请重新扫码');
        return false;
    } else {
        return {qrKey:key,type:type};
    }
}

// 轮播图
function getSliders() {
    var success = function (res) {
        var slider = res.data;
        if(slider.waterbill_list !=undefined && slider.waterbill_list != null && slider.waterbill_list!='' && slider.waterbill_list.length > 0){
            $('.has_ticket').removeClass('none');
            $('.no_ticket').addClass('none');
            $('.change_bac').css('background','#fff');

            template.defaults.imports.slider_nums = slider.waterbill_list.length - 1;
            var html = template('slider_img',slider);
            $('#slider_img_content').append(html);
            var gallery = mui('.mui-slider');
            gallery.slider({
                interval:0//自动轮播周期，若为0则不自动播放，默认为0；
            });

            //最大可增加数 默认为轮播图的第一个对应参数
            max_num = slider.waterbill_list[0].rest_num;
            $('#tickt_id').val(slider.waterbill_list[0].id);

            //监听轮播图的切换
            document.querySelector('.mui-slider').addEventListener('slide', function(event) {
                //根据具体业务，动态获得 对应数据
                max_num = slider.waterbill_list[event.detail.slideNumber].rest_num;
                document.getElementById('max-button').removeAttribute("disabled");
                $('#tickt_id').val(slider.waterbill_list[event.detail.slideNumber].id);
                //监听轮播切换时 input中的数字是否超过当前余票多大值
                var num = $('#mui-numbox-input').val();
                if(num >= max_num){
                    if(max_num <= 0){
                        $('#mui-numbox-input').val(1);
                    }else {
                        $('#mui-numbox-input').val(max_num);
                    }
                    document.getElementById('max-button').setAttribute("disabled","disabled");
                    document.getElementById('min-button').removeAttribute("disabled");

                }
            });

            //绑定加 事件
            $('#max-button').on('tap',function () {
                console.log(max_num)
                //获取 输入框的数据
                var num = $('#mui-numbox-input').val();
                if(num >= max_num){
                    document.getElementById('max-button').setAttribute("disabled","disabled");
                    document.getElementById('min-button').removeAttribute("disabled");
                    if(max_num <= 0){
                        $('#mui-numbox-input').val(1);
                    }else {
                        $('#mui-numbox-input').val(max_num);
                    }
                }
            })

        }else {
            $('.has_ticket').addClass('none');
            $('.no_ticket').removeClass('none');
            $('.change_bac').css('background','#f1f1f1');
        }

        $("#mui-numbox-input").bind('input porpertychange',function(){
            var num = $('#mui-numbox-input').val();
            if(num >= max_num){
                if(max_num <= 0){
                    $('#mui-numbox-input').val(1);
                }else {
                    $('#mui-numbox-input').val(max_num);
                }
                document.getElementById('max-button').setAttribute("disabled","disabled");
                document.getElementById('min-button').removeAttribute("disabled");
            }
        });
    };
    getData(share_water_bill,combineParams({qrKey:keyData.qrKey}),success);
}

//请求默认地址 以及 地址列表
var success = function (res) {

    var info = res.data;
    var html = template('address',info);
    //默认地址
    var address = "";
    var addr = "";
    if(info.address_list != undefined && info.address_list != null && info.address_list != '' && info.address_list.length > 0){
        $('.show_noaddress').removeClass('none');
        for(var i=0;i<info.address_list.length;i++){
            if(info.address_list[i].is_default != undefined
                &&info.address_list[i].is_default != null
                && info.address_list[i].is_default !=''
                && info.address_list[i].is_default == 1)
            {
                address = info.address_list[i];
                if(address == undefined || address == null || address == ''){
                    html = '<p class="to_addAddress"><span style="color: rgba(0,0,0,.2)">暂无收货地址</span></p>';
                    $("#button").attr("disabled",true);
                    malert('无收货地址，无法下单');
                }else {
                    addr = address.area_info+" "+address.address;
                    $("#button").removeAttr("disabled");
                }
                continue;
            }
        }
    }else {
        $('.show_noaddress').css('display','block');
        html = '<p class="to_addAddress"><span style="color: rgba(0,0,0,.7)">暂无收货地址</span></p>'
        $("#button").attr("disabled",true);
        malert('无收货地址，无法下单');
    }

    $('#address_content').append(html);
    $('#address_detail').text(addr);


    //地址列表
    var html = template('address_list',info);
    $('#address_list_content').append(html);
    $('.choosed').addClass('none');
    if(info.address_list != undefined && info.address_list != null && info.address_list != ''){
        for(var i=0;i<info.address_list.length;i++){
            if(info.address_list[i].is_default != undefined
                &&info.address_list[i].is_default != null
                && info.address_list[i].is_default !=''
                && info.address_list[i].is_default == 1)
            {
                $('.choosed'+info.address_list[i].id).removeClass('none');
            }
        }
    }

    //监听遮罩层的点击事件
    mui('.list_ocntent').on('tap', '.ad_list', function () {
        var sPrents = $(this).parents(".list_ocntent");
        var address_id = sPrents.attr('id');
        console.log(address_id)
        if(info.address_list != undefined && info.address_list != null && info.address_list != ''){
            for(var i=0;i<info.address_list.length;i++) {
                info.address_list[i].is_default = 0;
                $('.choosed').addClass('none');
                if(info.address_list[i].id == address_id){
                    address = info.address_list[i];
                    $('.choosed'+address_id).removeClass('none');
                    info.address_list[i].is_default = 1;
                    var address_detail = info.address_list[i];
                    $('.about-name-new').text(address_detail.name);
                    $('.about_mobile').text(address_detail.mobile);
                    var addr = address_detail.area_info+" "+address_detail.address;
                    $('#address_detail').text(addr);
                    break;
                }
            }
        }
        offCanvasWrapper.offCanvas().close();
    });


    //显示或隐藏遮罩层
    $('.show_zhezhao').on('tap',function () {
        offCanvasWrapper.offCanvas('show');
    })


    //绑定减去事件
    $('#min-button').on('tap',function () {
        var num = $('#mui-numbox-input').val();
        if(num =1){
            $('#mui-numbox-input').text(1);
        }
        if(num > 3){
            return;
        }
        console.log(11234)
    })


    //点击确定订水 调用方法
    document.getElementById("button").addEventListener('tap', function() {
        //获取订的票数
        var buy_num = $('#mui-numbox-input').val();
        //弹框
        var btnArray = ['取消', '确定'];
        mui.confirm('你确定要购买'+buy_num+'桶水？', '提示', btnArray, function(e) {
            if (e.index == 1) {
                //获取要传入的参数
                var id = $('#tickt_id').val();
                var num = buy_num;
                var buyer_name = address.name;
                var mobile = address.mobile;
                var buyer_address = address.area_info +" "+address.position_address+address.address;
                var description = $('#remark-input').val();

                var success = function (res) {
                    console.log(res);
                    //表示是下单的成功页面
                    setTimeout(function() {
                        redirect("share-success.html?type=" + 3);
                    }, 2000);

                }

                //调用一键订水购买接口
                postData(share_add_bill_order,combineParams({
                    id:id,
                    num:num,
                    buyer_name:buyer_name,
                    mobile:mobile,
                    buyer_address:buyer_address,
                    description:description,
                    qrKey:keyData.qrKey,
                }),success);

            }
        })
    });

}
getData(share_address_list,combineParams({qrKey:keyData.qrKey}),success);


//获取原始窗口的高度
var originalHeight=document.documentElement.clientHeight || document.body.clientHeight;
window.onresize=function(){

    //软键盘弹起与隐藏  都会引起窗口的高度发生变化
    var  resizeHeight=document.documentElement.clientHeight || document.body.clientHeight;
    if(resizeHeight*1<originalHeight*1){ //resizeHeight<originalHeight证明窗口被挤压了
        $('.sure-submit').css('display','none');
        $('.remark-input').addClass('pos_fixed');
        $('.bac_rgba').removeClass('none');

    }else{
        $('.sure-submit').css('display','block');
        $('.remark-input').removeClass('pos_fixed');
        $('.bac_rgba').addClass('none');
    }
}

$('.bac_rgba').on('tap',function () {
    $('.remark-input').removeClass('pos_fixed');
    $('.bac_rgba').addClass('none');
    $('.sure-submit').css('display','block');
    document.activeElement.blur();//关闭软键盘
})